//
//  ProductDetailCell.m
//  Peso Guide
//
//  Created by macbook on 2025/6/10.
//

#import "ProductDetailCell.h"

@interface ProductDetailCell ()

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *numberImg;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UIImageView *iconImg;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *rightImg;

@end

@implementation ProductDetailCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = ColorHex(@"#1A7A35");
        self.contentView.backgroundColor = ColorHex(@"#1A7A35");
        
        _topView = [PesoKit createViewWithFrame:CGRectMake(0, 0, KScreenWidth, 50) backGroundColor:ColorHex(@"#FFFFFF")];
        _topView.clipsToBounds = YES;
        _topView.layer.mask = [AppTools clipTopRightAndLeftByRoundingCorners:_topView radii:CGSizeMake(15.0f, 15.0f)];
        [self.contentView addSubview:_topView];
        
        UILabel*title = [PesoKit createLabelWithFrame:CGRectMake(0, 20, KScreenWidth, 22) backGroundColor:[UIColor clearColor] text:@"Certification" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 14)];
        title.textAlignment = NSTextAlignmentCenter;
        [_topView addSubview:title];
        
        CGSize titSize = [AppTools sizeWithText:title.text font:FONT(FontWeightStyleRegular, 16) maxSize:CGSizeMake(KScreenWidth, 22)];
        
        UIView *lineView = [PesoKit createViewWithFrame:CGRectMake((KScreenWidth-titSize.width)/2.0, title.bottom+4, ceil(titSize.width), 2) backGroundColor:ColorHex(@"#1A7A35")];
        [_topView addSubview:lineView];
        
        _backView = [PesoKit createViewWithFrame:CGRectMake(0, 50, KScreenWidth, 64) backGroundColor:ColorHex(@"#FFFFFF")];
        [self.contentView addSubview:_backView];
        
        _numberImg = [PesoKit createImageViewWithFrame:CGRectMake(24, 17, 30, 30) imageNamed:@"ic_product_no_number"];
        [_backView addSubview:_numberImg];
        
        _numberLabel = [PesoKit createLabelWithFrame:CGRectMake(0, 0, 30, 30) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#FFFFFF") font:FONT(FontWeightStyleRegular, 14)];
        _numberLabel.textAlignment = NSTextAlignmentCenter;
        [_numberImg addSubview:_numberLabel];
        
        _iconImg = [PesoKit createImageViewWithFrame:CGRectMake(_numberImg.right+10, 17, 30, 30) imageNamed:@""];
        [_backView addSubview:_iconImg];
        
        _titleLabel = [PesoKit createLabelWithFrame:CGRectMake(_iconImg.right+3, 0, 300, 64) backGroundColor:[UIColor clearColor] text:@"" textColor:ColorHex(@"#0E1D31") font:FONT(FontWeightStyleRegular, 16)];
        [_backView addSubview:_titleLabel];
        
        _rightImg = [PesoKit createImageViewWithFrame:CGRectMake(KScreenWidth-40-7, 28, 7, 12) imageNamed:@"ic_product_right_icon"];
        [_backView addSubview:_rightImg];
    }
    return self;
}

- (void)setDataDic:(NSDictionary *)dataDic {
    _dataDic = dataDic;
    
    if (_theIndex == 0) {
        _topView.hidden = NO;
        _backView.top = 50;
        self.backgroundColor = ColorHex(@"#1A7A35");
        self.contentView.backgroundColor = ColorHex(@"#1A7A35");
        
    }else {
        _topView.hidden = YES;
        _backView.top = 0;
        self.backgroundColor = ColorHex(@"#FFFFFF");
        self.contentView.backgroundColor = ColorHex(@"#FFFFFF");
    }
    
    NSString *accidental = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"accidental"]];
    NSString *callbacks = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"callbacks"]];
    //是否已完成 【重要】
    NSString *packages = [NSString stringWithFormat:@"%@",[dataDic safeObjectForKey:@"packages"]];

    _titleLabel.text = accidental;
    [_iconImg sd_setImageWithURL:[NSURL URLWithString:callbacks]];
    
    if ([packages isEqualToString:@"1"]) {
        _numberImg.image = [UIImage imageNamed:@"ic_product_duihao"];
        _numberLabel.text = @"";
    }else {
        _numberImg.image = [UIImage imageNamed:@"ic_product_no_number"];
        _numberLabel.text = [NSString stringWithFormat:@"%ld",(long)_theIndex+1];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
